package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.entity.HobbyVO;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.PersonalInfoXML;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/7 18:00
 * 修改备注：
 * ================================================
 */
public class HobbyActivity extends BaseActivity {


    @Bind(R.id.hobby_flow_layout)
    TagFlowLayout hobbyFlowLayout;

    private List<HobbyVO> hobbyVOList = new ArrayList<>();
    private TagAdapter<HobbyVO> tagAdapter;

    int[] select;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_hobby);
        setTitle("兴趣爱好");
        setRightText("保存");
    }

    @Override
    public void initMember() {

        initData();

        tagAdapter = new TagAdapter<HobbyVO>(hobbyVOList) {
            @Override
            public View getView(FlowLayout parent, int position, HobbyVO hobbyVO) {
                TextView tv = (TextView) mInflater.inflate(R.layout.view_text_view,
                        hobbyFlowLayout, false);
                tv.setText(hobbyVO.getHobbyName());
                return tv;
            }
        };

        hobbyFlowLayout.setAdapter(tagAdapter);
        tagAdapter.setSelectedList(select);

    }

    /**
     * 初始化数据
     */
    private void initData() {

        // 展示所有兴趣爱好
        hobbyVOList = JSON.parseArray(PersonalInfoXML.getAllHobbiesJson(this), HobbyVO.class);


        // 设置已经选中的兴趣爱好
        String myHobbies[] = strToArray(PersonalInfoXML.getHobbiesCode(this));

        if (myHobbies == null || myHobbies.length == 0)
            return;
        List<Integer> integerList = new ArrayList<>();

        for (int i = 0; i < hobbyVOList.size(); i++)
            for (int j = 0; j < myHobbies.length; j++)
                if (myHobbies[j].equals(hobbyVOList.get(i).getHobbyCode())) {
                    integerList.add(i);
                    hobbyVOList.get(i).setCheck(true);
                }

        select = new int[integerList.size()];
        for (int i = 0; i < integerList.size(); i++)
            select[i] = integerList.get(i);


    }


    @Override
    public void addListener() {

        hobbyFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (hobbyVOList.get(position).isCheck())
                    hobbyVOList.get(position).setCheck(false);
                else
                    hobbyVOList.get(position).setCheck(true);
                return false;
            }
        });

    }

    @Override
    public void setHandler() {

    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

        if (StringUtil.isStrEmpty(getHobbiesCode(hobbyVOList))){
            showToast("请至少选择一项");
            return;
        }

        Intent intent = new Intent();
        intent.putExtra("type", (String) getIntent().getSerializableExtra("type"));
        intent.putExtra("hobbiesName", getHobbiesName(hobbyVOList));
        intent.putExtra("hobbiesCode", getHobbiesCode(hobbyVOList));
        setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent，当然也可以自己定义新的Bundle
        finish();//此处一定要调用finish()方法
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 字符串转换成数据
     *
     * @param str
     * @return
     */
    public String[] strToArray(String str) {
        //拆分字符为"," ,然后把结果交给数组strArray
        return str.split(";");
    }

    /**
     * 字符串数组转整型数组
     *
     * @param strArray
     * @return
     */
    public int[] strArrayToIntArray(String[] strArray) {
        int[] intArray = new int[strArray.length];
        for (int i = 0; i < intArray.length; i++) {
            intArray[i] = Integer.parseInt(strArray[i]);
        }
        return intArray;
    }

    /**
     * 获取兴趣爱好List 返回字符串集合
     *
     * @param hobbiesBeen
     * @return
     */
    private String getHobbiesName(List<HobbyVO> hobbiesBeen) {
        String hobbiesName = "";

        if (hobbiesBeen != null && hobbiesBeen.size() > 0)
            for (int i = 0; i < hobbiesBeen.size(); i++)
                if (hobbiesBeen.get(i).isCheck())
                    hobbiesName += hobbiesBeen.get(i).getHobbyName() + ";";

        return hobbiesName;
    }
    /**
     * 获取兴趣爱好List 返回Code集合
     *
     * @param hobbiesBeen
     * @return
     */
    private String getHobbiesCode(List<HobbyVO> hobbiesBeen) {
        String hobbiesCode = "";

        if (hobbiesBeen != null && hobbiesBeen.size() > 0)
            for (int i = 0; i < hobbiesBeen.size(); i++)
                if (hobbiesBeen.get(i).isCheck())
                    hobbiesCode += hobbiesBeen.get(i).getHobbyCode() + ";";

        return hobbiesCode;
    }
}
